package hehe.aop.base;

import hehe.aop.Aop;
import net.sf.cglib.proxy.MethodInterceptor;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 代理类回调
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class ProxyCallback implements MethodInterceptor
{
    /**
     * 委托类
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    private Object target;

    public ProxyCallback(Object target)
    {
        this.target = target;
    }

    /**
     * 代理回调业务处理
     *<B>说明：</B>
     *<pre>
     *  执行各通知点的行为
     *</pre>
     * @param proxy 代理对象
     * @param method 委托类方法
     * @param args 方法参数
     * @param methodProxy 代理对象方法
     */
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable
    {
        String expression = this.target.getClass().getName() + "." + method.getName();
        Aspect aspect = Aop.getAspect(expression);
        Aop.triggerInterceptor(aspect, Aop.BEFORE_EVENT,this.target);
        Object result = null;
        try {
            result = methodProxy.invokeSuper(proxy, args);//调用目标类的目标方法
            Aop.triggerInterceptor(aspect, Aop.AFTER_EVENT,this.target);
        } catch (Exception ex) {
            Aop.triggerInterceptor(aspect, Aop.THROW_EVENT,this.target);
            throw ex;
        } finally {
            Aop.triggerInterceptor(aspect, Aop.FINALLY_EVENT,this.target);
        }

        return result;
    }
}
